package com.huike.web.controller;

import com.github.pagehelper.PageInfo;
import com.huike.entity.Department;
import com.huike.entity.Employee;
import com.huike.query.EmployeeQuery;
import com.huike.service.DepartmentService;
import com.huike.service.EmployeeService;
import com.huike.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("list")
    public ModelAndView getList(@ModelAttribute("qo") EmployeeQuery qo, ModelAndView mav){

        PageInfo<Employee> page = employeeService.getPage(qo);

        List<Department> departments = departmentService.getAll();

        mav.addObject("pageInfo",page);
        mav.addObject("departments",departments);

        mav.setViewName("/employee/list");
        return mav;
    }

    @RequestMapping("/input")
    public String addEmp(Model model,Integer id){
        Employee emp = employeeService.getEmpByID(id);
        model.addAttribute("employee",emp);
        model.addAttribute("departments",departmentService.getAll());
        model.addAttribute("roles",roleService.getAll());
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] roleIds){
        employeeService.saveOrUpdate(employee,roleIds);
        return "redirect:/employee/list";
    }


    @RequestMapping("/delete")
    public String delete(Long id){
        employeeService.deleteById(id);
        return "redirect:/employee/list";
    }


}
